package com.youyiwen.Controller;

import com.youyiwen.Bean.Question;
import com.youyiwen.Bean.User;
import com.youyiwen.Mapper.QuestionMapper;
import com.youyiwen.Service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.UUID;

/**
 * @Author: zhaoyang
 * @Date: 2021/03/16
 */
@Controller
public class PublishController {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private QuestionService questionService;
    @GetMapping("/publish")
    public String getPublish() {

        return "publish";
    }

    @PostMapping("/publish")
    public String postPublish(
            @RequestParam("title") String title,
            @RequestParam("description") String description,
            @RequestParam("tag") String tag,
            @RequestParam("questionID") String questionID,
            HttpSession session,
            Model model
    ) {
        User user = (User) session.getAttribute("loginUser");

        if (user == null) {
            model.addAttribute("msg", "请您登录后再进行发帖操作");
            return "login";
        }

        model.addAttribute("title", title);
        model.addAttribute("description", description);

        if (title == null || title == "" || description == null || description == "") {
            model.addAttribute("msg", "标题和内容都要补全才能找到答案哦！");
            return "publish";
        }


        Question question = questionMapper.getById(questionID);
        //首次发布
        if (question == null) {

            question = new Question();
            question.setUserName(user.getUserName());
            question.setTitle(title);
            question.setDescription(description);
            question.setTag(tag);
            question.setLikeCount(0);
            question.setViewCount(1);
            question.setCommentCount(0);
            question.setGmtCreate(System.currentTimeMillis());
            question.setGmtModify(question.getGmtCreate());
            question.setId(UUID.randomUUID().toString());
            // insert
            questionMapper.insertQuestion(question);
            return "redirect:/communityCenter";
        }
        questionMapper.updateQuestion(title,description,questionID);
        return "redirect:/communityCenter";
    }
    @GetMapping("/publish/{id}")
    public String edit(@PathVariable(name = "id") String id,
                       Model model){
        Question question = questionMapper.getById(id);
        if (question == null){
            return "communityCenter";
        }
        model.addAttribute("title",question.getTitle());
        model.addAttribute("description",question.getDescription());
        model.addAttribute("tag",question.getTag());
        model.addAttribute("questionID",question.getId());
        return "publish";
    }
}
